//
//  BaseViewController.m
//  quickBatchManager
//
//  Created by jack on 15/9/15.
//  Copyright (c) 2015年 jack. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()
{
    UILabel *titleLable;
    MBProgressHUD *HUD;
}

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [self configUI];
    [self initUI];
    
   
    
}


-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"blank"] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    [self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:@"blank"]];
    [self hideTabBar];
   
   
    
}


-(void)configUI
{
    if(![NSStringFromClass([self class]) isEqualToString:@"LoginViewController"])
    {
        if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
            self.edgesForExtendedLayout = UIRectEdgeNone;
        }
    }
    self.view.backgroundColor=BACKGROUND_COLOR;
    
    [self.navigationItem hidesBackButton];
    self.navigationController.navigationBarHidden=YES;
    
}

-(instancetype)init
{
    self=[super init];
    if(self)
    {
        if(titleLable==nil)
        {
            titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
        }
        [titleLable setTextColor:[UIColor whiteColor]];
        titleLable.font = [UIFont boldSystemFontOfSize:18];
        titleLable.textAlignment=NSTextAlignmentCenter;
        self.navigationItem.titleView = titleLable;
        titleLable.hidden=YES;
    }
    return self;
}



-(void) addNavBack
{
    UIButton *backBtn=[[UIButton alloc] initWithFrame:CGRectMake(marginSize, marginSize, 95, 35)];
    [backBtn setImage:[UIImage imageNamed:@"fanhui_1"] forState:UIControlStateNormal];
    backBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    
    [backBtn bk_whenTapped:^{
        [self back];
    }];
    
    [self.view addSubview:backBtn];
}


-(void)setTitle:(NSString *)title
{
    [super setTitle:title];
    self.navigationItem.titleView.hidden=YES;
}
-(void)initData
{
    
}
-(void)initUI
{
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
-(void)back
{
    if([self.navigationController.topViewController isEqual:[self.navigationController.viewControllers objectAtIndex:0]])
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    else [self.navigationController popViewControllerAnimated:YES];
}


//选择相册
-(void)pickPhoto
{
        UIImagePickerController *imgPicker = [UIImagePickerController new];
        imgPicker.delegate = self;
        imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:imgPicker animated:YES completion:nil];
}

//选择相机
-(void)pickCamera
{
    UIImagePickerController *imgPicker = [UIImagePickerController new];
    imgPicker.delegate = self;
    imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentViewController:imgPicker animated:YES completion:nil];
}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    
    //    if(_isCrop)
    //    {
    //        //添加到集合中
    //        UIImage * photo =[ToolUtils scaleToSize:(UIImage*)[info objectForKey:UIImagePickerControllerOriginalImage]];
    //
    //
    //        PECropViewController *controller = [[PECropViewController alloc] init];
    //
    //        CGFloat width = photo.size.width;
    //        CGFloat height = photo.size.height;
    //        CGFloat length = MIN(width, height);
    //        controller.imageCropRect = CGRectMake((width - length) / 2,
    //                                              (height - length) / 2,
    //                                              length,
    //                                              length);
    //        controller.keepingCropAspectRatio=YES;
    //
    //        controller.delegate = self;
    //        controller.image = photo;
    //        controller.toolbarHidden=YES;
    //
    //        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    //            self.navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
    //        }
    //
    //
    //        [self.navigationController pushViewController:controller animated:YES];
    //    }
}


//#pragma mark - PECropViewControllerDelegate methods
//
//-(void) cropViewController:(PECropViewController *)controller didFinishCroppingImage:(UIImage *)croppedImage
//{
//    [self.navigationController popViewControllerAnimated:YES];
//}
//
//- (void)cropViewControllerDidCancel:(PECropViewController *)controller
//{
//    // [controller dismissViewControllerAnimated:YES completion:NULL];
//    [self.navigationController popViewControllerAnimated:YES];
//}



- (void)hideTabBar {
    
    UIView *contentView;
    if ( [[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] )
    {
        contentView = [self.tabBarController.view.subviews objectAtIndex:1];
    }
    else
    {
        contentView = [self.tabBarController.view.subviews objectAtIndex:0];
    }
    contentView.frame = CGRectMake(contentView.bounds.origin.x,  contentView.bounds.origin.y,  contentView.bounds.size.width, contentView.bounds.size.height + self.tabBarController.tabBar.frame.size.height);
    self.tabBarController.tabBar.hidden = YES;
    [self.tabBarController.tabBar setFrame:CGRectMake(0, 0, 0, 0)];
    
}

-(void) showAlert:(NSString *)content{
    UIAlertView *alert=[UIAlertView bk_alertViewWithTitle:@"提示" message:content];
    [alert bk_setCancelButtonWithTitle:@"确定" handler:^{
        
    }];
    [alert show];
}


-(void) showAlert:(NSString *)content handler:(void (^)(void))block{
    UIAlertView *alert=[UIAlertView bk_alertViewWithTitle:@"提示" message:content];
    [alert bk_setCancelButtonWithTitle:@"确定" handler:block];
    [alert show];
}


#pragma -textField 文字变化
-(void) addTextFieldChange:(UITextField*)textField
{
    [textField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
}

-(void)textFieldChanged:(id)sender
{
    //    UITextField *ctextfield=(UITextField*)sender;
    //    if(ctextfield.maxLength>0 && ctextfield.text.length>ctextfield.maxLength)
    //    {
    //        ctextfield.text=[ctextfield.text substringToIndex:ctextfield.maxLength];
    //    }
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}


//自动布局快速添加
- (void)setEdge:(UIView*)supperView firstView:(UIView*)firstView secondView:(UIView*)secondView attr:(NSLayoutAttribute)attr constant:(CGFloat)constant
{
    if(secondView==nil)
    [supperView addConstraint:[NSLayoutConstraint constraintWithItem:firstView attribute:attr relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:constant]];
    else [supperView addConstraint:[NSLayoutConstraint constraintWithItem:firstView attribute:attr relatedBy:NSLayoutRelationEqual toItem:secondView attribute:attr multiplier:1.0 constant:constant]];
}



-(void)showProgressHUD{
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.labelText = @"正在努力加载中...";
}
-(void)showProgressHUD:(NSString *)msg {
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.labelText = msg;
}
-(void)hideProgressHUD{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

-(void)showToast:(NSString *)text{
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.labelText = text;
    HUD.mode = MBProgressHUDModeText;
    
    //指定距离中心点的X轴和Y轴的偏移量，如果不指定则在屏幕中间显示
    //    HUD.yOffset = 150.0f;
    //    HUD.xOffset = 100.0f;
    
    [HUD showAnimated:YES whileExecutingBlock:^{
        sleep(2);
    } completionBlock:^{
        [HUD removeFromSuperview];
        HUD = nil;
    }];
}


-(void)reloadData:(ChangeType)changeType
{
    
}

@end
